package com.example.myrecyclerview.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.baidu.mapapi.CoordType;
import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.Marker;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.map.TextOptions;
import com.baidu.mapapi.model.LatLng;
import com.example.myrecyclerview.MyConst;
import com.example.myrecyclerview.R;
import com.example.myrecyclerview.ShopLoader;
import com.example.myrecyclerview.bean.Shop;

import java.util.ArrayList;
import java.util.List;

public class MapViewFragment extends Fragment implements Runnable{

    private Handler handler;
    private MapView mapView;
    private List<Shop> shops;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View root = inflater.inflate(R.layout.fragment_mapview, container, false);
        mapView = (MapView) root.findViewById(R.id.mapView);

        //初始化地图
        initMapView();
        //初始化handler
        initHandler();
        //开启网络访问线程
        Thread webThread = new Thread(this);
        webThread.start();

        return root;
    }

    private void initMapView() {
        //      初始中心位置，v相当于维度，v1相当于经度
        LatLng centerPoint = new LatLng(22.255895,113.541058);
//      设置地图初始的范围，越高越精确
        MapStatus mapStatus = new MapStatus.Builder().target(centerPoint).zoom(17.0f).build();
        MapStatusUpdate mMapStatusUpdate = MapStatusUpdateFactory.newMapStatus(mapStatus);
        BaiduMap baiduMap = mapView.getMap();
        baiduMap.setMapStatus(mMapStatusUpdate);

//         marker事件和响应
        BitmapDescriptor bitmapDescriptor= BitmapDescriptorFactory.fromView(getActivity().getLayoutInflater().inflate(R.layout.layout_icon_jnu,null));
        MarkerOptions markerOption=new MarkerOptions();
        markerOption.position(centerPoint).icon(bitmapDescriptor);
        Marker maker= (Marker) baiduMap.addOverlay(markerOption);

        OverlayOptions textOption = new TextOptions().bgColor(0xAAAAFF00).fontSize(25).fontColor(0xFFFF00FF).text(getString(R.string.map_school)).rotate(0).position(centerPoint);
        baiduMap.addOverlay(textOption);

        baiduMap.setOnMarkerClickListener((arg0)->{
            Toast.makeText(getContext(), getString(R.string.map_tips_click), Toast.LENGTH_SHORT).show();
            return false;
        });
    }

    private void initHandler() {
        handler = new Handler(){
            @Override
            public void handleMessage(@NonNull Message msg) {
                switch (msg.what){
                    case MyConst.UPDATE_MAP:
                        for(Shop shop:shops){
                            markMap(shop);
                        }
                        break;
                }
            }
        };
    }

    private void markMap(Shop shop) {
        Log.d("<Test>","shop:"+shop.toString());
        //定义Maker坐标点
        LatLng point = new LatLng(shop.getLatitude(), shop.getLongitude());
        //构建Marker图标
        BitmapDescriptor bitmap = BitmapDescriptorFactory
                .fromResource(R.drawable.ic_marka);
        //构建MarkerOption，用于在地图上添加Marker
        OverlayOptions option = new MarkerOptions()
                .position(point)
                .icon(bitmap);
        //在地图上添加Marker，并显示
        BaiduMap baiduMap = mapView.getMap();
        baiduMap.addOverlay(option);
    }

    @Override
    public void onResume() {
        super.onResume();
        mapView.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        mapView.onPause();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mapView.onDestroy();
    }

    @Override
    public void run() {

        ShopLoader shopLoader = new ShopLoader();
        shops = shopLoader.download();

        Message message = new Message();
        message.what = MyConst.UPDATE_MAP;
        message.obj = shops;
        handler.sendMessage(message);

//        for(Shop shop:shops){
//            Log.d("<Test>", shop.toString());
//        }
    }


}
